﻿namespace AntdUI_HamburgerMenuTabs.Forms
{
    public partial class FullScreen : AntdUI.Window
    {
        private AntdUI.FormFloatButton? fbClose = null;

        public FullScreen()
        {
            InitializeComponent();

            FormBorderStyle = FormBorderStyle.None;
            ShowInTaskbar = false;
            WindowState = FormWindowState.Maximized;

            // 此处设置 Padding 的主要目的是为了让窗体 MouseMove 事件能够被触发
            Padding = new(5);

            Shown += (s, e) =>
            {
                ShowFloatButton();
            };

            MouseMove += (s, e) =>
            {
                // 因窗体内部控件会遮挡窗体 Padding 后的区域，故此处可变相实现鼠标靠边自动显示悬浮按钮
                if (fbClose != null)
                {
                    if (!fbClose.Visible) fbClose.Show();
                }
            };
        }

        /// <summary>
        /// 在全屏窗口中提供悬浮按钮，以便能够操作退出全屏
        /// </summary>
        private void ShowFloatButton()
        {
            AntdUI.FloatButton.ConfigBtn[] btns =
            [
                new AntdUI.FloatButton.ConfigBtn("btnClose", "CompressOutlined", true){
                    Tooltip = "退出全屏（Esc 或 F11）",
                    Type = AntdUI.TTypeMini.Primary,
                },
                new AntdUI.FloatButton.ConfigBtn("btnHide", "EyeInvisibleFilled", true){
                    Tooltip = "隐藏（或显示）按钮（F12）",
                    Type = AntdUI.TTypeMini.Primary,
                },
            ];

            fbClose = AntdUI.FloatButton.open(new AntdUI.FloatButton.Config(this, btns, btn =>
            {
                switch (btn.Name)
                {
                    case "btnClose":
                        Close();
                        break;
                    case "btnHide":
                        fbClose?.Hide();
                        break;
                }
            })
            {
                MarginX = 0,
                MarginY = 0,
                Gap = 20,
            });

            fbClose!.KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.F11 || e.KeyCode == Keys.Escape)
                {
                    Close();
                }
            };
        }

        /// <summary>
        /// 全局快捷键
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Close();
                    break;
                case Keys.F11:
                    Close();
                    break;
                case Keys.F12:
                    if (fbClose != null)
                    {
                        if (fbClose.Visible)
                        {
                            fbClose.Hide();
                        }
                        else
                        {
                            fbClose.Show();
                        }
                    }
                    break;
            }
            return false;
        }
    }
}